﻿Public Class ctrSetores
    Inherits ctrPadrao

#Region "Declaração de variáveis"
    Private objSetores As New Persistencia.perSetores
    Private iIdSetor As Int32
#End Region

#Region "Propriedades"

    Public Property IDSetor() As Int32
        Get
            Return Me.iIdSetor
        End Get
        Set(ByVal value As Int32)
            Me.iIdSetor = value
        End Set
    End Property

    Public ReadOnly Property sqlConsulta() As String
        Get
            Return Me.objSetores.sqlConsulta
        End Get
    End Property

#End Region

#Region "Métodos públicos"

    Public Function salvar(ByVal iIdSetor As Int32, _
                           ByVal sSetor As String, _
                           ByVal bRealizaVisitas As Boolean, _
                           ByVal bRealizaContatos As Boolean) As Boolean

        Dim bResultado As Boolean = False

        If iIdSetor > 0 Then
            Me.alterarSetor(iIdSetor, _
                            sSetor, _
                            bRealizaVisitas, _
                            bRealizaContatos)
            Me.IDSetor = iIdSetor
            bResultado = True
        Else
            Me.IDSetor = Me.inserirSetor(sSetor, _
                                         bRealizaVisitas, _
                                         bRealizaContatos)
            bResultado = True
        End If

        Return bResultado

    End Function

    Public Function excluir(ByVal iIdSetor As Int32) As Boolean
        Dim bResultado As Boolean = True

        If bResultado Then
            Me.excluirSetor(iIdSetor)
        End If

        Return bResultado

    End Function

    Private Sub excluirSetor(ByVal iIdSetor As Int32)

        Try
            Me.objSetores.excluirSetores(iIdSetor)
        Catch ex As Exception
            Throw New Exception("Exceção no método 'excluirSetor'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Sub

    Public Function selecionarSetor(ByVal iIDSetor As Int32) As DataTable
        Try
            Return Me.objSetores.selecionarSetor(iIDSetor)
        Catch ex As Exception
            Throw New Exception("Exceção no método 'selecionarSetores'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try
    End Function

    Public Function selecionarSetores(ByVal iIdSetor As Int32) As DataRow
        Try
            Return Me.objSetores.selecionarSetores(iIdSetor)
        Catch ex As Exception
            Throw New Exception("Exceção no método 'selecionarSetores'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try
    End Function

    Public Function retornarCampoSetores(ByVal sCampo As String, _
                                         ByVal iIdSetor As Int32) As DataRow

        Try
            Dim dtDados As New DataTable

            dtDados = Me.objSetores.selecionarCampoSetores(sCampo, _
                                                           iIdSetor)
            If Not dtDados Is Nothing Then
                Return dtDados.Rows(0)
            Else
                Return Nothing
            End If
        Catch ex As Exception
            Throw New Exception("Exceção no método 'retornarCampoSetores'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function

#End Region

#Region "Métodos privados"

    Private Sub alterarSetor(ByVal iIdSetor As Int32, _
                             ByVal sSetor As String, _
                             ByVal bRealizaVisitas As Boolean, _
                             ByVal bRealizaContatos As Boolean)
        Try
            Me.objSetores.alterarSetores(iIdSetor, _
                                         sSetor, _
                                         bRealizaVisitas, _
                                         bRealizaContatos)
        Catch ex As Exception
            Throw New Exception("Exceção no método 'alterarSetor'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Sub

    Private Function inserirSetor(ByVal sSetor As String, _
                                  ByVal bRealizaVisitas As Boolean, _
                                  ByVal bRealizaContatos As Boolean)

        Dim iIdSetor As Int32

        Try
            iIdSetor = Me.objSetores.inserirSetores(sSetor, _
                                                    bRealizaVisitas, _
                                                    bRealizaContatos)

            Return iIdSetor

        Catch ex As Exception
            Throw New Exception("Exceção no método 'inserirSetor'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function

#End Region
End Class
